package com.llt.springcloud.controller;

import com.llt.springcloud.pojo.Dept;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;

@RestController
public class DeptConsumerController {
    //消费者不应该有server层
    @Autowired
    private RestTemplate restTemplate;
//    private static final String REST_URL_PREFIX="http://127.0.0.1:8001";
    //ribbon 需要通过服务名来获取
    private static final String REST_URL_PREFIX="http://SPRINGCLOUD-PROVIDER-DEPT";
    @PostMapping("/consumer/dept/add")
    public boolean add(Dept dept){
        System.out.println(dept.getDname());
        return restTemplate.postForObject(REST_URL_PREFIX+"/dept/add",dept,Boolean.class);
    }
    @GetMapping("/consumer/dept/get")
    public Dept get(Long id){
        return restTemplate.getForObject(REST_URL_PREFIX+"/dept/getById?id="+id,Dept.class);
    }
    @GetMapping("/consumer/dept/list")
    public List<Dept> getDeptAll(){
        return restTemplate.getForObject(REST_URL_PREFIX+"/dept/list",List.class);
    }
}
